;(function($, ECar) {
	"use strict";
//ios btn的active属性生效
	var a=document.getElementsByTagName('a');for(var i=0;i<a.length;i++){a[i].addEventListener('touchstart',function(){},false)}

// swiper初始化
	var swiper = new Swiper('#slides', {
		pagination : '#slides .swiper-pagination',
		onInit: function(swiper){
			var activeIndex=swiper.activeIndex+1;
			var imagesLoaded=swiper.imagesLoaded;
			$(".num_pagination .pagination-current").text(activeIndex);
			$(".num_pagination .pagination-total").text(imagesLoaded);
		},
		onSlideChangeEnd: function(swiper){
			var index=swiper.activeIndex+1;
			$(".num_pagination .pagination-current").text(index);
		}
	})

	var oCart = {
// 增加商品数量+
		addSkuNum: function(self) {
			var that = this,
				jGoodNum = self.prev(),
				jBtnReduce = self.prevAll(".btn_reduce"),
				sCount = jGoodNum.val();

			if (self.hasClass("disabled")) return;

			sCount = parseInt(that.handleSkuNum(sCount)) + 1;

			if (sCount === 2) {
				jBtnReduce.removeClass("disabled");
			} else if (that.checkSkuNum(sCount) !== true) { //校验库存是否允许
				self.addClass("disabled");
			}
			jGoodNum.val(sCount);
		},

		// 减少商品数量-
		reduceSkuNum: function(self) {
			var that = this,
				jGoodNum = self.next(),
				jBtnAdd = self.nextAll(".btn_add"),
				sCount = jGoodNum.val();

			sCount = parseInt(that.handleSkuNum(sCount));

			if (self.hasClass("disabled") || sCount === 1) return;

			if (sCount === 2) {
				self.addClass("disabled");
			}

			jGoodNum.val(sCount - 1);
			jBtnAdd.removeClass("disabled");
		},

		// 处理商品数量
		handleSkuNum: function(count) {
			var nFlag = this.validSkuNum(count);
			if (nFlag === 0) {
				return count.replace("0", "");
			} else if (nFlag === -1) {
				return 1;
			} else {
				return count;
			}
		},

		// 校验商品数量是否符合规则
		validSkuNum: function(count) {
			if (/^[1-9][0-9]?$/.test(count)) {
				return 1;
			} else if (/^0[1-9]$/.test(count)) {
				return 0;
			} else {
				return -1;
			}
		},

		// 校验商品数量是否满足库存
		checkSkuNum: function(count) {
			var nResult = 3;
			if (count >= nResult) {
				return nResult;
			} else {
				return true;
			}
		},
	}
//商品数量操作
	$(".btn_number").on("click", function(e) {
		var oTarget = e.target || e.srcElement,
			sTagName = oTarget.nodeName.toLowerCase(),
			nFlag = 1,
			sCount = "",
			sType = "";
		if (sTagName !== "a") {
			return;
		}

		sType = $(oTarget).data("type");
		sCount = $("#good_number").val();
		nFlag = validNumber(sCount);
		if (nFlag === 0) {
			sCount = sCount.replace("0", "");
		} else if (nFlag === -1) {
			$("#good_number").val("1");
			return;
		}

		sCount = parseInt(sCount);
		switch (sType) {
			case "reduce":
				if (sCount > 1) {
					sCount = sCount - 1;
				}
				break;
			case "add":
				if (sCount < 99) {
					sCount = sCount + 1;
				}
		}
		$("#good_number").val(sCount);
	});

//商品数量输入控制
	$("#good_number").on("keyup", function(e) {
		var sKeyCode = e.which || e.keyCode,
			sCount = $(this).val(),
			nFlag = validNumber(sCount);

		if ((sKeyCode >= 96 && sKeyCode <= 105) || (sKeyCode >= 48 && sKeyCode <= 57)) {
			return;
		}
		sCount = sCount.slice(0, 1);
		$(this).val(sCount);
	});
	$("#good_number").on("change", function(e) {
		var sCount = $(this).val(),
			nFlag = validNumber(sCount);

		switch (nFlag) {
			case 0:
				sCount = sCount.replace("0", "");
				break;
			case -1:
				sCount = "1";
				break;
			default:
				return;
		}

		$(this).val(sCount);
	});

/**
 * [校验商品数量格式是否正确]
 */
	function validNumber(sCount) {
		if (/^[1-9][0-9]?$/.test(sCount)) {
			return 1;
		} else if (/^0[1-9]$/.test(sCount)) {
			return 0;
		} else {
			return -1;
		}
	}
})(Zepto, window.ECar || (window.ECar = {}));